package com.pucheng.bracelet.ui.activity.steps

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.base.BaseHistoryToDateActivity
import com.pucheng.bracelet.app.ext.KEY_DATE
import com.pucheng.bracelet.app.ext.KEY_STEPS
import com.pucheng.bracelet.app.util.CacheUtil
import com.pucheng.bracelet.viewmodel.sleep.SleepViewModel
import com.pucheng.bracelet.viewmodel.steps.StepsViewModel
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class StepsHistoryActivity : BaseHistoryToDateActivity<StepsViewModel>() {

    override fun titleResId(): Int = R.string.steps_history


    override fun colorId(): Int = R.color.colorAccent

    override fun createObserver() {
        super.createObserver()
        mViewModel.allDataToStep.observe(this@StepsHistoryActivity, Observer { it1 ->
            val map: MutableMap<String, Calendar> =
                HashMap()
            val tager: Int = CacheUtil.getStepsFlag().toInt()
            for (stepsBean in it1) {
                val date = stepsBean.date.split("-")
                val schemeCalendar = getSchemeCalendar(
                    date[0].toInt(),
                    date[1].toInt(),
                    date[2].toInt(),
                    (stepsBean.stepsNum.toInt() / tager * 100).toString()
                )
                map[schemeCalendar.toString()!!] = schemeCalendar!!

            }
            //此方法在巨大的数据量上不影响遍历性能，推荐使用
            mCalendarView!!.setSchemeDate(map)
        })
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mCalendarView!!.setOnCalendarSelectListener(object : CalendarView.OnCalendarSelectListener {
            override fun onCalendarSelect(calendar: Calendar?, isClick: Boolean) {
                LogUtils.e("year:${calendar!!.year} month:${calendar!!.month } day:${calendar!!.day}")
                val mIntent = Intent()
                mIntent.putExtra(KEY_DATE,"${calendar!!.year}-${if (calendar!!.month<10){"0"+calendar!!.month}else{calendar!!.month} }-${if (calendar!!.day<10){"0"+calendar!!.day}else{calendar!!.day} }")
//                setResult(Activity.RESULT_OK,mIntent)
                mIntent.setClass(this@StepsHistoryActivity,StepsActivity::class.java)
                startActivity(mIntent)
                finish()
            }

            override fun onCalendarOutOfRange(calendar: Calendar?) {
            }
        })
    }

}